<!--{__NOLAYOUT__}-->
{include file="../application/admin/view/template/header.html" /}
    <form class="layui-form" style="width:90%" onsubmit="return false">
        <div class="layui-form-item">
            <label class="layui-form-label">主菜单</label>
            <div class="layui-input-block">
                <select name="main_menu" lay-filter="main_menu" lay-verify="required">
                    <option value="">请选择一个主菜单</option>
                    {foreach $menuData as $k => $v}
                        <option value="{$v['menu_id']}">{$v['title']}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">子菜单</label>
            <div class="layui-input-block">
                <select name="son_menu" lay-filter="son_menu" id="son_menu">
                    <option value="">请选择一个子菜单</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-block">
                <input type="text" value="" name="title" placeholder="请输入菜单名称" autocomplete="off"
                       class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">url</label>
            <div class="layui-input-block">
                <input type="text" value="" name="url" placeholder="请输入访问地址" autocomplete="off"
                       class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单图标</label>
            <div class="layui-input-block">
                <input type="text" value="" name="icon" placeholder="请输入菜单图标" autocomplete="off"
                       class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序id</label>
            <div class="layui-input-block">
                <input type="number" value="" name="sort_id" placeholder="排序id" autocomplete="off"
                       class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addDBConf">立即提交</button>
            </div>
        </div>
    </form>
<script>
    layui.use(['form', 'laydate', 'jquery'], function () {
        var form = layui.form,
            $ = layui.jquery;

        //监听提交
        form.on('submit(addDBConf)', function (data) {
            var data = data.field;
            data['parent_id'] = data['son_menu'] ? data['son_menu'] : data['main_menu'];
            $.post(location.href, data, function (result) {
                layer.msg(result.msg);
                if (result.status) {
                    window.setTimeout(parent.layer.closeAll, 1000);
                }
            });
            return false;
        });

        //菜单二级联动
        form.on('select(main_menu)', function (data) {
            $.post('/admin/menu/getSonMenu', {menu_id: data.value}, function (result) {
                if (result.status) {
                    var html = '<option value="0">请选择一个子菜单</option>';
                    $.each(result.data, function (index, element) {
                        html += '<option value="'+element.menu_id+'">'+element.title+'</option>';
                    });
                    $("#son_menu").html(html);
                    form.render('select');
                }
            })
        });
    });
</script>